package com.kcwx.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.kcwx.common.annotation.Log;
import com.kcwx.common.core.controller.BaseController;
import com.kcwx.common.core.domain.AjaxResult;
import com.kcwx.common.core.domain.entity.SysDictData;
import com.kcwx.common.core.page.TableDataInfo;
import com.kcwx.common.enums.BusinessType;
import com.kcwx.common.utils.StringUtils;
import com.kcwx.common.utils.poi.ExcelUtil;
import com.kcwx.system.service.ISysDictDataService;
import com.kcwx.system.service.ISysDictTypeService;

import com.kcwx.shareddomain.common.dto.DeleteDto;
import com.kcwx.shareddomain.common.dto.DelParaValid;
import com.kcwx.shareddomain.system.query.SysDictDataQuery;
import com.kcwx.shareddomain.system.dto.SysDictDataEditDto;
import com.kcwx.system.dtomap.SysDictDataMap;

/**
 * 数据字典信息
 * 
 */
@Api(tags = "系统管理-字典管理")
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController {
	@Autowired
	private ISysDictDataService dictDataService;

	@Autowired
	private ISysDictTypeService dictTypeService;

	@Autowired
	private  SysDictDataMap sysDictDataMap;
	/**
	 * 新增字典
	 */
	@ApiOperation("新增")
	@PreAuthorize("@ss.hasPermi('system:dict:add')")
	@Log(title = "新增字典", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	public AjaxResult add(@Validated @RequestBody SysDictDataEditDto dto) {
		SysDictData dict=sysDictDataMap.AddDtoToEntity(dto);
		dict.setCreateBy(getUsername());
		//验证字典是否存在
		int rowsAffect=dictDataService.insertDictData(dict);
		return rowsAffect>0?success():error("新增失败");
	}

	/**
	 * 修改保存字典
	 */
	@ApiOperation("修改")
	@PreAuthorize("@ss.hasPermi('system:dict:edit')")
	@Log(title = "修改字典", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	public AjaxResult edit(@Validated @RequestBody SysDictDataEditDto dto) {
		SysDictData dict=sysDictDataMap.EditDtoToEntity(dto);
		dict.setUpdateBy(getUsername());
		//验证字典是否存在
		int rowsAffect=dictDataService.updateDictData(dict);
		return rowsAffect>0?success():error("修改失败");
	}

	/**
	 * 删除字典
	 */
	@ApiOperation("删除字典")
	@PreAuthorize("@ss.hasPermi('system:dict:remove')")
	@Log(title = "删除字典", businessType = BusinessType.DELETE)
	@DeleteMapping("/remove")
	public AjaxResult remove(@RequestBody DeleteDto dto) {
		Long id=DelParaValid.validDel(dto);
		int rowsAffect=dictDataService.deleteDictDataById(id,getUsername());
		return rowsAffect>0? success():error("删除失败");
	}

	/**
	 * 批量删除字典
	 */
	@ApiOperation("批量删除字典")
	@PreAuthorize("@ss.hasPermi('system:dict:remove')")
	@Log(title = "批量删除字典", businessType = BusinessType.DELETE)
	@DeleteMapping("/batchRemove")
	public AjaxResult batchRemove(@RequestBody DeleteDto dto) {
		Long []ids=DelParaValid.validBatchDel(dto);
		int rowsAffect=dictDataService.deleteDictDataByIds(ids,getUsername());
		return rowsAffect>0? success():error("删除失败");
	}
	/**
	 * 分页查询
	 */
	@ApiOperation("分页查询")
	@PreAuthorize("@ss.hasPermi('system:dict:list')")
	@Log(title = "字典分页查询", businessType = BusinessType.OTHER)
	@PostMapping("/list")
	public AjaxResult list(@RequestBody SysDictDataQuery dictData) {
		startPage();
		List<SysDictData> list = dictDataService.selectDictDataList(dictData);
		return success(getDataTable(list));
	}
	/**
	 * 根据id获取详细信息
	 */
	@ApiOperation("获取详细信息")
	@PreAuthorize("@ss.hasPermi('system:dict:query')")
	@Log(title = "根据id获取字典", businessType = BusinessType.OTHER)
	@GetMapping("/getById")
	public AjaxResult getInfo(Long id) {
		return success(dictDataService.selectDictDataById(id));
	}
	/**
	 * 根据字典类型查询字典数据信息
	 */
	@ApiOperation("根据字典类型查询字典数据信息")
	@Log(title = "根据字典类型查询字典数据信息", businessType = BusinessType.OTHER)
	@GetMapping(value = "/getDict")
	public AjaxResult getDict(String type) {
		List<SysDictData> data = dictTypeService.selectKeyValueByType(type);
		if (StringUtils.isNull(data)) {
			data = new ArrayList<SysDictData>();
		}
		return success(data);
	}

	/**
	 * 根据字典类型查询字典数据信息
	 */
	@ApiOperation("根据字典类型查询字典数据信息")
	@Log(title = "根据字典类型查询字典数据信息", businessType = BusinessType.OTHER)
	@GetMapping("/type")
	public AjaxResult dictType(String dictType) {
		List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
		if (StringUtils.isNull(data)) {
			data = new ArrayList<SysDictData>();
		}
		return success(data);
	}
}
